package com.zaofan.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.zaofan.bean.GoodsOrderInfo;
import com.zaofan.bean.OrderInfo;
import com.zaofan.constant.StateConstant;
import com.zaofan.dao.base.DaoSupportImpl;

public class GoodsOrderInfoDao extends DaoSupportImpl<GoodsOrderInfo> {

	public List<GoodsOrderInfo> findByAcccountId(Integer accountId, Integer begin, Integer end) {
		Map<String, Object> params = new HashMap<>();
		params.put("accountId", accountId + "");
		params.put("begin", begin + "");
		params.put("end", end + "");
		params.put("isTimeOut", "0");
		params.put("order", " by orderTime desc,id desc");
		return findAll(params);
	}

	public GoodsOrderInfo findById(Integer orderId) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", orderId);
		return findOne(params);
	}

	public void delOrder(OrderInfo info) {

	}

	public void updateaOrderPayState(String orderNo) {
		try {
			String sql = "update " + GoodsOrderInfo.class.getSimpleName().toLowerCase() + " set state = "
					+ StateConstant.ORDER_STATE_PAY + " where orderNo = '" + orderNo + "'";
			System.err.println(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			runner.update(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public boolean isFirstOrder(int id) {
		try {
			String sql = "select count(*) from " + GoodsOrderInfo.class.getSimpleName().toLowerCase()
					+ " where accountId = " + id + " and state in (1,2) and payState in (2,3) and isTimeOut = 0";
			System.err.println(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			Long count = runner.query(sql, new ScalarHandler<Long>());
			if (count == 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

}
